package com.ming.closeto.ui;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ming.closeto.R;
import com.ming.closeto.adapter.GalleryAdapter;
import com.ming.closeto.utils.GlideUtils;
import com.ming.closeto.utils.LogUtils;
import com.ming.closeto.utils.OkHttpUtils;
import com.ming.closeto.utils.SystemUiUtils;

import java.io.IOException;
import java.util.ArrayList;

/**
 * 画廊
 */

public class GalleryActivity extends Activity implements View.OnClickListener {

    private Gallery mGallery;
    private ImageView iv_back;
    private TextView tv_set_wallpaper;
    private TextView tv_down;

    private GalleryAdapter mGalleryAdapter;
    private WallpaperManager mWallpaperManager;

    private int position;
    private ArrayList<String> mListBig = new ArrayList<>();

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        // 沉浸式
        //SystemUiUtils.showWindowUI(this);

        initView();
    }

    private void initView() {
        mWallpaperManager = WallpaperManager.getInstance(this);

        Intent intent = getIntent();
        position = intent.getIntExtra("position", 0);
        mListBig = intent.getStringArrayListExtra("bigUrl");

        mGallery = findViewById(R.id.mGallery);
        mGalleryAdapter = new GalleryAdapter(this, mListBig);
        mGallery.setAdapter(mGalleryAdapter);

        iv_back = findViewById(R.id.iv_back);
        tv_set_wallpaper = findViewById(R.id.tv_set_wallpaper);
        tv_down = findViewById(R.id.tv_down);

        iv_back.setOnClickListener(this);
        tv_set_wallpaper.setOnClickListener(this);
        tv_down.setOnClickListener(this);

        if (mListBig.size() > 0) {
            mGallery.setAdapter(mGalleryAdapter);
            mGallery.setSelection(position);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_down:
                downloadImg();
                break;
            case R.id.tv_set_wallpaper:
                /**
                 * 1. 设置桌面壁纸
                 * 2. 设置锁屏壁纸
                 */
                setDesktopWallpaper();
                break;
        }
    }

    // 设置桌面壁纸
    private void setDesktopWallpaper() {
        GlideUtils.loadBitmap(this, mListBig.get(mGallery.getSelectedItemPosition()), new GlideUtils.OnSimpleTargetListener() {
            @Override
            public void onResourceReady(Bitmap resource) {
                try {
                    mWallpaperManager.setBitmap(resource);
                    Toast.makeText(GalleryActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    Toast.makeText(GalleryActivity.this, "设置失败" + e.toString(), Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    private static String PATH = Environment.getExternalStorageDirectory() + "/";

    private void downloadImg() {
        OkHttpUtils.downlaod(PATH + System.currentTimeMillis() + ".png",
                mListBig.get(mGallery.getSelectedItemPosition()),
                new OkHttpUtils.OnOkHttpCallback() {
                    @Override
                    public void onFailure(IOException e) {
                        Toast.makeText(GalleryActivity.this, "下载失败1:" + e.toString(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(String msg) {
                        Toast.makeText(GalleryActivity.this, "下载失败2:" + msg, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccessful(String json) {
                        Toast.makeText(GalleryActivity.this, "下载成功", Toast.LENGTH_SHORT).show();
                    }
                },
                new OkHttpUtils.OnProgressResponseListener() {
                    @Override
                    public void OnprogressResponse(int progress, long total) {
                        LogUtils.e("progress:" + progress + "total:" + total);
                    }
                });
    }
}
